#version 460

layout(local_size_x = 1024, local_size_y = 1, local_size_z = 1) in;

uniform int kernel_w, kernel_h;
uniform float x, y;
uniform int coef_n;
shared int WorkGroupSize;
shared int col_t, row_t;
shared int index_texel;

layout(rgba32f, binding = 0) uniform image2D kernel;

layout(std430, binding = 0) buffer KernelCoef
{
	vec4 data[];
} kernel_coef;

void main() {
	int GlobalInvocationIndex = int(gl_WorkGroupID.x * gl_WorkGroupSize.x + gl_LocalInvocationID.x);
	if (GlobalInvocationIndex == 0)
	{
		WorkGroupSize = int(gl_WorkGroupSize.x * gl_WorkGroupSize.y * gl_WorkGroupSize.z);
		col_t = int(x);
		row_t = int(kernel_h - y);
		index_texel = row_t * kernel_w + col_t;
	}
	barrier();
	// Clear iamge
	for (int i = GlobalInvocationIndex; i < kernel_h * kernel_w; i += WorkGroupSize)
	{
		int row = i / kernel_w;
		int col = i % kernel_w;
		imageStore(kernel, ivec2(col, row), vec4(0, 0, 0, 0));
	}
	barrier();
	// imageStore(kernel, ivec2(col_t + 10, row_t), vec4(1, 0, 0, 1));
	for (int i = GlobalInvocationIndex; i < coef_n; i += WorkGroupSize)
	{
		int index = int(kernel_coef.data[index_texel * coef_n + i].y);
		int row = index / kernel_w;
		int col = index % kernel_w;
		imageStore(kernel, ivec2(col, row), vec4(1, 0, 0, 1));
	}
	barrier();
}
